package gui;

import info.*;
import sql.Connect;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import java.sql.SQLException;

public class Register {
    RegistrationForm RstForm = new RegistrationForm();

    private void emptyInput(JTextField Textinput){
              String input = Textinput.getText();
              String regex = "^请输入";
              // 创建 Pattern 对象
              Pattern pattern = Pattern.compile(regex);
              // 创建 Matcher 对象
              Matcher matcher = pattern.matcher(input);
              // 检查是否匹配
              boolean matches = matcher.find();
              // 输出结果
              if(matches){
                  Textinput.setText("");
              }
    }
    private void  RegisterQuery(Connect conn){
        if(RstForm.getCode().equals(RstForm.getCode(0))){
            try {
                int result =  conn.Register(RstForm);
                if(result>0){
                    JOptionPane.showMessageDialog(null, "注册成功，请返回登录", "注册完成 ", JOptionPane.INFORMATION_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null, "注册失败", "错误 ", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }else{
            JOptionPane.showMessageDialog(null, "验证码错误", "错误 ", JOptionPane.ERROR_MESSAGE);
        }
    }
    public JPanel creatRegister(CardLayout cardLayout, JPanel mainPanel, Connect conn){
        // 创建页面2
        JPanel RegisterPage = new JPanel(new GridLayout(2,1));
        //欢迎组件
        JLabel WelText = new JLabel("欢迎注册");
        WelText.setHorizontalAlignment(SwingConstants.CENTER);

        //注册容器
        JPanel InputBox = new JPanel(new GridLayout(5,1));//输入组件的父容器
        JPanel NumberInput = new JPanel();//账号输入容器
        JPanel NameInput = new JPanel();//密码输入容器
        JPanel PwdInput = new JPanel();//密码输入容器
        JPanel EmailInput = new JPanel();//邮箱输入容器
        JPanel verifyInput = new JPanel();//验证码输入框
        //注册输入框
        JLabel UserText = new JLabel("用户名");
        JTextField UserInput = new JTextField("请输入学号或教职工编号",20);
        NumberInput.add(UserText);
        NumberInput.add(UserInput);

        //姓名输入框
        JLabel nameText = new JLabel("姓  名");
        JTextField nameInput = new JTextField("请输入姓名",20);
        NameInput.add(nameText);
        NameInput.add(nameInput);

        JLabel PasswordText = new JLabel("密   码");
        JPasswordField PasswordInput = new JPasswordField("",20);
        PwdInput.add(PasswordText);
        PwdInput.add(PasswordInput);

        JLabel EmailText = new JLabel("邮   箱");
        JTextField EmlInput = new JTextField("请输入邮箱",20);
        EmailInput.add(EmailText);
        EmailInput.add(EmlInput);

        JLabel VerifyText = new JLabel("验证码");
        JTextField VryInput = new JTextField("请输入验证码",8);
        JButton VryBtn = new JButton("发送验证码");
        verifyInput.add(VerifyText);
        verifyInput.add(VryInput);
        verifyInput.add(VryBtn);
        //发送验证码按钮


        //功能按钮容器
        JPanel FunBox = new JPanel();
        JButton RgsBtn =  new JButton("注册");
        JButton goToPage1 = new JButton("已有账号，回到登录");

        //获取用户输入的内容
        UserInput.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateNum();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateNum();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateNum();
            }
            private void updateNum() {
                String text = UserInput.getText();
                if (text == "null" || text.isEmpty()) {
                    RstForm.setNumber(""); //设置为默认值
                    return;
                }
                try {
                    RstForm.setNumber(text);
                    System.out.println("Text changed: " + text);
                } catch (NumberFormatException ex) {
                    System.err.println("Invalid number format: " + text);
                    // 处理无效格式的情况
                }
            }
        });
        UserInput.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                emptyInput(UserInput);
            }
        });
        nameInput.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateName();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateName();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateName();
            }
            private void updateName() {
                String text = nameInput.getText();
                if (text == "null" || text.isEmpty()) {
                    RstForm.setName(""); //设置为默认值
                    return;
                }
                try {
                    RstForm.setName(text);
                    System.out.println("Text changed: " + text);
                } catch (NumberFormatException ex) {
                    System.err.println("Invalid number format: " + text);
                    // 处理无效格式的情况
                }
            }
        });
        nameInput.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                emptyInput(nameInput);
            }
        });
        PasswordInput.getDocument().addDocumentListener(new DocumentListener(){
            @Override
            public void insertUpdate(DocumentEvent e) {
                updatePwd();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updatePwd();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updatePwd();
            }
            private void updatePwd() {
                RstForm.setPwd(PasswordInput.getText());
                System.out.println(" RstForm.setPwd:"+ RstForm.getPwd());
            }
        });
        EmlInput.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateEmail();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateEmail();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateEmail();
            }
            public  void updateEmail(){
                RstForm.setEmail(EmlInput.getText());
                System.out.println(" LoginForm.setPwd:"+ RstForm.getEmail());
            }
        });
        EmlInput.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                emptyInput(EmlInput);
            }
        });
        VryInput.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateCode();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateCode();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateCode();
            }
            public  void updateCode(){
                RstForm.setCode(EmlInput.getText());
                System.out.println(" RstForm.setCode:"+ RstForm.getCode());
            }
        });
        VryInput.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                emptyInput(VryInput);
            }
        });
        //注册按钮监听事件
        RgsBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            //private String[] Roles={"Stu","Teacher","Counsellor"};
            //根据输入的学号或教职工编号来判断角色
                String inputRole = RstForm.getNumber();
                char firstChar = inputRole.charAt(0);
                System.out.println("firstChar:"+firstChar);
                switch (firstChar){
                    case '1':RstForm.setRole("Counsellor");break;
                    case '2':RstForm.setRole("Stu");break;
                    case '3':RstForm.setRole("Teacher");break;
                };
                RegisterQuery(conn);
            }
        });

        goToPage1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(mainPanel, "Login");
            }
        });

        InputBox.add(NumberInput);
        InputBox.add(NameInput);
        InputBox.add(PwdInput);
        InputBox.add(EmailInput);
        InputBox.add(verifyInput);
        FunBox.add(RgsBtn);
        FunBox.add(goToPage1);
        InputBox.add(FunBox);
        //添加到面板
        RegisterPage.add(WelText,BorderLayout.NORTH);
        RegisterPage.add(InputBox,BorderLayout.CENTER);

        return RegisterPage;
    }

}
